package com.yycx.module.bus.provider.service.hy.impl;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.query.CriteriaDelete;
import com.yycx.module.bus.client.entity.hy.BusPaymentOrder;
import com.yycx.module.bus.provider.mapper.hy.BusPaymentOrderMapper;
import com.yycx.module.bus.provider.service.hy.BusBankStatementService;
import com.yycx.module.bus.provider.service.hy.BusPaymentOrderService;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.query.CriteriaQuery;
import org.springframework.transaction.annotation.Propagation;
import com.yycx.common.mybatis.query.CriteriaSave;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 认款使用记录接口实现类
 *
 * @author flyme
 * @date 2023-11-04
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusPaymentOrderServiceImpl extends BaseServiceImpl<BusPaymentOrderMapper, BusPaymentOrder> implements BusPaymentOrderService {

    @Autowired
    @Lazy
    private BusPaymentService paymentService;

    @Autowired
    private BusBankStatementService bankStatementService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusPaymentOrder bpo, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusPaymentOrder> cq, BusPaymentOrder bpo, EntityMap requestMap) {
        cq.orderByDesc("bpo.createTime");
        return ResultBody.ok();
    }

    @Override
    public ResultBody beforeDelete(CriteriaDelete<BusPaymentOrder> cd) {
        Serializable paymentOrderId = cd.getIdValue();
        BusPaymentOrder paymentOrder = getById(paymentOrderId);
        if (FlymeUtils.isNotEmpty(paymentOrder)) {
            Integer amountType = paymentOrder.getAmountType();
            //实时认款删除认款单
            if (amountType.equals(1)) {
                Long paymentId = paymentOrder.getPaymentId();
                if (FlymeUtils.isNotEmpty(paymentId)) {
                    paymentService.removeById(paymentId);
                }
                String bankStatementId = paymentOrder.getBankStatementId();
                if (FlymeUtils.isNotEmpty(bankStatementId)) {
                    bankStatementService.clearPaymentId(bankStatementId);
                }
            }
            //提前认款取消认款状态
            if (amountType.equals(3)) {

            }

        }
        return super.beforeDelete(cd);
    }

    @Override
    public BusPaymentOrder findByPaymentIdAndOrderId(String orderId, Long paymentId) {
        CriteriaQuery cq = new CriteriaQuery(BusPaymentOrder.class);
        cq.eq(true, "orderId", orderId);
        cq.eq(true, "paymentId", paymentId);
        return getOne(cq, false);
    }

    @Override
    public BigDecimal totalByPaymentId(Long paymentId) {
        CriteriaQuery<BusPaymentOrder> cq = new CriteriaQuery(BusPaymentOrder.class);
        cq.select("sum(useAmount)");
        cq.eq(true, "paymentId", paymentId);
        BigDecimal useAmount = getObj(cq, e -> new BigDecimal(e.toString()));
        return FlymeUtils.getBigDecimal(useAmount, new BigDecimal("0"));
    }

    @Override
    public Boolean removeByOrderId(String orderId) {
        CriteriaDelete cd = new CriteriaDelete();
        cd.eq(true, "orderId", orderId);
        return remove(cd);
    }

    @Override
    public List<BusPaymentOrder> selectByOrderId(String orderId) {
        CriteriaQuery cq = new CriteriaQuery(BusPaymentOrder.class);
        cq.eq(true, "orderId", orderId);
        return list(cq);
    }

    @Override
    public List<BusPaymentOrder> selectByOrderIdOrderByAmountAsc(String orderId) {
        CriteriaQuery cq = new CriteriaQuery(BusPaymentOrder.class);
        cq.eq(true, "orderId", orderId);
        cq.orderByAsc("useAmount");
        return list(cq);
    }
}
